/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import pojo.BenhNhan;

/**
 *
 * @author Thanh Minh
 */
public class BenhNhanDAO {
    
  //lay danh sach benh nhan  
public static ArrayList<BenhNhan> layDSBN(){
    ArrayList<BenhNhan> ds = new ArrayList<BenhNhan>();
    try{
    MysqldataAccesshelper helper = new MysqldataAccesshelper();
        helper.open();
        String sql = "SELECT * FROM benhnhan ";
        try{
        ResultSet rs = helper.executeQuery(sql);
        while(rs.next()){
            BenhNhan  bn= new BenhNhan();
            int MaBN  = rs.getInt("MaBN");
            String hoten = rs.getString("HoTen");
            int gioitinh = rs.getInt("GioiTinh");
            String DiaChi = rs.getString("DiaChi");
            int namsinh = rs.getInt("NamSinh");
            
            bn.setMaBN(MaBN);
            bn.setHoTen(hoten);
            bn.setGioiTinh(gioitinh);
            bn.setNamSinh(namsinh);
            bn.setDiaChi(DiaChi);
            
            ds.add(bn);
        }
        helper.close();
        }
        catch(SQLException e ){
            System.out.println(e);
        }
    }
     catch(SQLException e ){
            System.out.println(e);
        }
    return ds;
}
    
 public static int themBenhNhan(BenhNhan BN){     
        int n=-1;
        try{
        MysqldataAccesshelper helper = new MysqldataAccesshelper();
        helper.open();
         String sql=String.format("INSERT INTO benhnhan (MaBN,HoTen,GioiTinh,NamSinh,DiaChi)VALUES (%d,'%s',%d,%d,'%s')",BN.getMaBN(),BN.getHoTen(),BN.getGioiTinh(),BN.getNamSinh(),BN.getDiaChi() );
         n = helper.executeUpdate(sql);        
        helper.close();
        }
        catch(SQLException ex){
            System.out.println("Them that bai"+ ex.getMessage());
        }  
         return n;
    }
 public static int chinhSuaThongTinBenhNhan(int mabn , String Hoten , int gioitinh, int namsinh ,String Diachi ){
     int n=-1;
     try{
          MysqldataAccesshelper helper = new MysqldataAccesshelper();
          helper.open();
         String sql=String.format(" UPDATE benhnhan SET HoTen = '%s' , GioiTinh=%d , NamSinh = %d , DiaChi = '%s'  WHERE MaBN=%d",Hoten,gioitinh,namsinh,Diachi,mabn);
         n = helper.executeUpdate(sql);        
        helper.close();
     }
     catch(SQLException e )
     {
         System.out.println("update thong tin benh nhan  that bai"+e.getMessage());
     }
     return n;
 }
 public static int xoaBenhNhan(int MaBN){
     int n=-1;
     try{
          MysqldataAccesshelper helper = new MysqldataAccesshelper();
          helper.open();
         String sql=String.format(" DELETE FROM benhnhan WHERE MaBN=%d",MaBN);
         n = helper.executeUpdate(sql);        
        helper.close();
     }
     catch(SQLException e )
     {
         System.out.println("khong xoa duoc "+e.getMessage());
     }
     return n;
 }
 
 
}
